package dao

import (
	"context"
	"prod/api/prod"
	"prod/internal/model"
)

func (d *Dao) LoadGroupList(ctx context.Context, sgId, pStoreId uint32, nature prod.Nature) (
	groups []*model.Group, err error) {
	db := d.TransContext(ctx).Model(&model.Group{}).Where(
		"sg_id = ? AND p_store_id = ? AND status = ?",
		sgId, pStoreId, true)
	if nature != prod.Nature_NATURE_ALL {
		db = db.Where("nature = ?", int(nature))
	}
	if err = db.Scan(&groups).Error; err != nil {
		err = d.processSqlError(err)
	}
	return
}

func (d *Dao) GetGroupByName(ctx context.Context, name string, sgId, pStoreId uint32) (
	group *model.Group, err error) {
	group = &model.Group{}
	if err = d.TransContext(ctx).Take(group,
		"sg_id = ? AND p_store_id = ? AND name = ? AND status = ?",
		sgId, pStoreId, name, true).Error; err != nil {
		err = d.processSqlError(err)
	}
	return
}
